package controller;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.UrlUtility;

import model.BookingDTO;
import model.UserDTO;

import bizlogic.BookingManager;
import bizlogic.UserManager;

@WebServlet("/DetailBooking")
public class DetailBookingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public DetailBookingServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doShow(request, response);
	}
	
	private void doShow(HttpServletRequest req, HttpServletResponse res) {
		String bookId = req.getParameter("id");
		String address = "/WEB-INF/ShowBookingDetail.jsp";
		
		if ( bookId != null ) {
			BookingManager bm = new BookingManager();
			UserManager um = new UserManager();
			
			BookingDTO book = null;
			UserDTO user = null;
			UserDTO currentUser = (UserDTO)req.getSession().getAttribute("user");
			
			book = bm.getBooking(bookId);
			if ( book != null ) {
				user = um.getUser(book.getUserId());
				
				if ( currentUser.getType().equalsIgnoreCase("manager") &&
				   ( book.getStatus().equalsIgnoreCase("book") || 
					 book.getStatus().equalsIgnoreCase("update") ) && 
					 book.getStartDate().after(Calendar.getInstance().getTime()) ) {
						req.setAttribute("mode", "approve");
					
				} else if ( currentUser.getId().equals(user.getId()) &&
					 book.getStatus().equalsIgnoreCase("book") && 
					 book.getStartDate().after(Calendar.getInstance().getTime()) ) {
						req.setAttribute("mode", "delete");
				} else if ( currentUser.getId().equals(user.getId()) &&
						 book.getStatus().equalsIgnoreCase("approve") && 
						 book.getStartDate().after(Calendar.getInstance().getTime()) ) {
							req.setAttribute("mode", "cancel");
				}

				req.setAttribute("booking", book);
				req.setAttribute("ownerUser", user);
				
				address = "/WEB-INF/ShowBookingDetail.jsp";
				
				UrlUtility.forwardPage(req, res, address);
			} else {
				
			}
		} else {
			UrlUtility.redirectPage(res, "ViewCurrentBooking");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}
	
	private void doProcess(HttpServletRequest req, HttpServletResponse res) {
		String bookId = req.getParameter("bookId");
		String bookStatus = req.getParameter("bookStatus");
		
		System.out.println("ID: " + bookId + " | Status: " + bookStatus);
		
		String address = "";
		
		if ( bookId != null && bookStatus != null ) {
			BookingManager bm = new BookingManager();
			BookingDTO book = bm.getBooking(bookId);
			UserDTO user = (UserDTO)req.getSession().getAttribute("user");
			String error = "";
			String mode = "";
			
			if ( book != null ) {
				if ( ( bookStatus.equalsIgnoreCase("approve") || 
					   bookStatus.equalsIgnoreCase("reject") ) && 
					 ( book.getStatus().equalsIgnoreCase("book") || 
					 book.getStatus().equalsIgnoreCase("update") )
				   ) {
					book.setStatus(bookStatus);
					book.setValidateBy(user.getId());
					book.setValidateOn(Calendar.getInstance().getTime());
					
					mode = "approve";
					error = bm.updateBooking(book);
				} else if ( ( bookStatus.equalsIgnoreCase("delete") ) && 
						    ( book.getStatus().equalsIgnoreCase("book") ||
						      book.getStatus().equalsIgnoreCase("update") ) ) {
					book.setStatus(bookStatus);

					mode = "delete";
					error = bm.updateBooking(book);
				} else if ( bookStatus.equalsIgnoreCase("cancel") && 
					        book.getStatus().equalsIgnoreCase("approve") ) {
					book.setStatus(bookStatus);
					
					mode = "cancel";
					error = bm.updateBooking(book);
				}
				
				if ( !error.trim().isEmpty() ) {
					req.setAttribute("error", error);
					
					req.setAttribute("booking", book);
					req.setAttribute("mode", mode);
					
					address = "/WEB-INF/ShowBookingDetail.jsp";
					UrlUtility.forwardPage(req, res, address);
				} else {
					address = "ViewCurrentBooking";
					UrlUtility.redirectPage(res, address);
				}
			}
		}
		
	}

}
